﻿#include "hzpch.h"
#include "LayerStack.h"

namespace Hazal
{
	CLayerStack::CLayerStack() : m_unLayerInsertIdx(0)
	{
	}

	CLayerStack::~CLayerStack()
	{
		/// LayerStack贯穿程序运行的整个声明周期, 可以管理剩余layer的销毁操作
		for (CLayer* layer : m_vecLayers)
		{
			if (nullptr != layer)
			{
				delete layer;
			}
		}
	}

	void CLayerStack::PushLayer(CLayer* layer)
	{
		/// 在当前位置前插入layer
		m_vecLayers.emplace(m_vecLayers.begin() + m_unLayerInsertIdx, layer);
		++m_unLayerInsertIdx;

		layer->OnAttach();
	}

	void CLayerStack::PushOverlay(CLayer* overlay)
	{
		/// 在vec最后添加新layer, 相当于覆盖操作, 该layer在最后被渲染
		m_vecLayers.emplace_back(overlay);
		overlay->OnAttach();
	}

	void CLayerStack::PopLayer(CLayer* layer)
	{
		/// 弹栈的过程只是从vec中移除, 并不会真正的销毁该layer
		
		LayerVecIter it = std::find(m_vecLayers.begin(), m_vecLayers.end(), layer);

		if (it != m_vecLayers.end())
		{
			m_vecLayers.erase(it);
			--m_unLayerInsertIdx;
		}
	}

	void CLayerStack::PopOverlay(CLayer* overlay)
	{
		/// 弹栈的过程只是从vec中移除, 并不会真正的销毁该layer

		LayerVecIter it = std::find(m_vecLayers.begin(), m_vecLayers.end(), overlay);

		if (it != m_vecLayers.end())
		{
			m_vecLayers.erase(it);
		}
	}
}